package routers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/liangchao/blogo/src/common/middlewares"
	"github.com/liangchao/blogo/src/common/models"
	"github.com/liangchao/blogo/src/web/handler"
	"html/template"
	"time"
)

func SetupRouter(r *gin.Engine) {

	r.Static("/static", "static")
	r.Static("/css", "static/css")
	r.Static("/js", "static/js")
	r.Static("/assets", "static/assets")
	r.SetFuncMap(template.FuncMap{
		"add": func(i int) string{
			i += 1
			return fmt.Sprintf("%d", i)
		},
		"sub": func(i int) string{
			if i > 1 {
				i -= 1
			}
			return fmt.Sprintf("%d", i)
		},
		"isMaxPage": func(page int, pageSize int, total int64) bool {
			return total <= int64(page) * int64(pageSize)
		},
		"color": func(id int) string{
			color := []string{"bg-primary", "bg-secondary", "bg-success", "bg-danger", "bg-warning", "bg-info", "bg-light text-dark", "bg-dark"}
			return color[id % len(color)]
		},
		"Year": func(t time.Time) string{
			return t.Format("2006")
		},
		"Day": func(t time.Time) string {
			return t.Format("02")
		},
		"Calendar": func(bs []models.Blog) string{
			return bs[0].CreateTime.Format("2006-01")
		},
	})
	r.LoadHTMLGlob("views/**/*")
	r.StaticFile("/favicon.ico", "static/images/logo.png")

	r.Use(middlewares.GinLogger(), middlewares.GinRecovery(true))

	{
		r.GET("/", handler.GetHomePage) // 主页
		// r.GET("/home", handler.GetHomePage) // 主页
		r.GET("/archive", handler.GetArchivePage)
		r.GET("/contact", handler.GetContactPage) // 联系我页
		r.GET("/blog/query/:page/:limit", handler.QueryBlogListByKeywordsPage) // 搜索页
		r.GET("/blog/:bid/detail", handler.GetDetailPage) // 文章页


		r.POST("/message/add", handler.AddMessage) // 添加消息

	}

	//BlogRouter := r.Group("/blog")

}

